package com.sharp.core.raytrace;

import com.sharp.core.graphics.Color;
import com.sharp.core.graphics.Graphic;
import com.sharp.core.graphics.UnColor;
import com.sharp.core.graphics.gl.OpenGLGraphic;
import com.sharp.core.raytrace.shading.Shading;
import com.sharp.core.voxel.Voxel;
import com.sharp.core.voxel.VoxelIntersection;

public class VoxelTracer extends Tracer {

	
	public VoxelTracer(Camera cam, RayFactory caster,
			Shading shading, ObjectHolder objs) {
		super(cam, caster, shading, objs);
	}
	
	@Override
	public void render() {
		for ( int x = 0; x < cam.getWidth(); x++ ) {
			for ( int y = 0; y < cam.getHeight(); y++ ) {
				Ray ray = new Ray();
				caster.cast(ray, x, y);
				VoxelIntersection inter = (VoxelIntersection) objs.findIntersection(ray);
				UnColor c;
				if ( inter == null ) {
					c = Color.BLACK;
				} else { 
					Color c2 = new Color(inter.getVoxel().getColor()); 
					if ( inter.getSide() == Voxel.Side.LEFT )
						c2.multiply(0.9f);
					else if (inter.getSide() == Voxel.Side.TOP )
						c2.multiply(0.8f);
					c = c2;
				}
				((OpenGLGraphic) Graphic.instance()).drawToBackBuffer(cam, x, y, c);
			}
		}
	}
}
